package com.alibaba.citrus.cr.mn.order.center.facade.function.assemble.impl.returnorder;

import com.alibaba.citrus.cr.common.enums.GeneralStatusEnum;
import com.alibaba.citrus.cr.mn.order.center.facade.function.assemble.OrderBizAssemble;
import com.alibaba.citrus.cr.mn.order.center.facade.function.check.CommonCheck;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.mnordercenter.model.dto.Modifiers;
import com.epoch.app.mnordercenter.model.dto.ReturnOrderBizCreateRequest;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author zhangxiaorui
 * @ClassName OrderBizAssembleOfMember
 * @description: TODO
 * @date 2023/05/17 09:49
 * @version: 1.0
 */
@Service
@Order(1)
public class ReturnOrderBizAssembleOfMemberCenter implements OrderBizAssemble<ReturnOrderBizCreateRequest> {

    private static final Log log = Log.getLogger(ReturnOrderBizAssembleOfMemberCenter.class);
    @Resource
    private CommonCheck commonCheck;

    @Override
    public Map<String, Object> query(ReturnOrderBizCreateRequest sa) {
        return query(Lists.newArrayList());
    }

    @Override
    public Map<String, Object> query(List<ReturnOrderBizCreateRequest> returnOrderBizBatchCreateRequests) {
        try {
            if (CollectionUtils.isEmpty(returnOrderBizBatchCreateRequests)) {
                return Maps.newHashMap();
            }
            List<String> employeeCodes = Lists.newArrayList();
            for (ReturnOrderBizCreateRequest createRequest : returnOrderBizBatchCreateRequests) {
                if (StringUtils.isNotBlank(createRequest.getCreatorCode())) {
                    employeeCodes.add(createRequest.getCreatorCode());
                }
            }
            Map<String, Modifiers> memberInfoMap = commonCheck.getModifiers(employeeCodes);

            Map<String, Object> result = Maps.newHashMap();
            result.put(MEMBER_INFO, memberInfoMap);
            log.info("退货订单商业能力会员中心数据组装结果 result={}", JSONObject.toJSONString(result));
            return result;
        } catch (Exception e) {
            log.error("退货订单商业能力会员中心数据组装失败", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public Result<String> assemble(Map<String, Object> queryResponseMap, ReturnOrderBizCreateRequest createRequest) {
        // 用户信息
        Map<String, Modifiers> memberInfoMap = getBizInfoMap(queryResponseMap, MEMBER_INFO, Modifiers.class);

        // 用户
        Modifiers memberInfo = memberInfoMap.get(createRequest.getCreatorCode());
        if (Objects.isNull(memberInfo) || !GeneralStatusEnum.YES.getCode().equals(memberInfo.getStatus())) {
            return Result.fail(createRequest.getSourceOrderNo(),"OC-01-001-01-16-041", "创建人不存在");
        }
        createRequest.setCreatorCode(memberInfo.getWorkId());
        createRequest.setCreatorName(memberInfo.getName());

        return Result.success(null);
    }
}
